<?php

include('inc/class.sql.php');
include('inc/class.auth.php');
include('inc/db.php');

$sql_conn = new mysql($host,$db,$user,$pass);

session_start();
 
if (!isset($_SESSION['username'])) {

    header("Location: index.php");

} else {

    $username = $_SESSION['username'];
    $query = "select id from users where username = '$username';";
    $results = $sql_conn->select($query);

    foreach ($results as $row) {
        $user_id = $row['id'];
    }

} ?>

<html>
<title>To-do List</title>
<?php include('inc/theme.php'); ?>
<br />
<p style="font-size:40px;font-family:'Roboto Slab'"><?php print strtoupper($username."'s to do list"); ?></p>
  <form method="post">
    <table>
      <tr>
        <td>
          <input type="text" name="entry_name" placeholder="Title"><br />
        </td><td>
          <input type="text" name="description" placeholder="Description"><br />
        </td><td>
          <input type="text" name="due_date" placeholder="Due Date"><br />
        </td><td>
          <input type="submit" value="Submit">
        </td>
      </tr>
    </table>
  </form>

<?php if (!empty($_POST)) {

    $name = addslashes($_POST['entry_name']);
    $desc = addslashes($_POST['description']);
    $date = addslashes($_POST['due_date']);

    if (empty($name) && empty($desc)) { ?>

        <p style="font-size:15px;font-family:'Roboto Slab'">You must enter at least a title or description.</p>

<?php } else {

        if (empty($name)) {
            $name = "-";
        }

        if (empty($desc)) {
            $desc = "-";
        }

        if (empty($date)) {
            $date = "-";
        }

        $query = "insert into entries (user_id, completed, timestamp, entry_name, description, due_date)
              values ('$user_id',0,date_format(now(),'%a, %b %D %l:%i%p'),'$name','$desc','$date')";  
        $sql_conn->write($query);

    }

} else if (!empty($_GET['remove_id'])) {

    $rm_id = $_GET['remove_id'];

    $query = "delete from entries where id='$rm_id' limit 1;";
    $sql_conn->write($query);

} else if (!empty($_GET['complete_id'])) {

    $cmpl_id = $_GET['complete_id'];

    $query = "update entries set completed=1 where id='$cmpl_id' limit 1;";
    $sql_conn->write($query);

} else if (!empty($_GET['uncomplete_id'])) {

    $uncmpl_id = $_GET['uncomplete_id'];

    $query = "update entries set completed=0 where id='$uncmpl_id' limit 1;";
    $sql_conn->write($query);

/*** Logic for disabled "clear" option.
} else if ($_GET['clear'] == true) {

    $query = "truncate entries;";
    $sql_conn->write($query);
***/

}

$query = "select id, completed, timestamp, entry_name, description, due_date from entries where user_id='$user_id' order by completed;";
$results = $sql_conn->select($query);

if (!$results) { ?>

<p style="font-size:15px;font-family:'Roboto Slab'">Add a new item above.</p>

<?php 
} else {
?>

<br />
<table class="curvedEdges" cellspacing="0px">
  <tr bgcolor="#FFFFFF">
    <td></td><td>
      <b>Added</b>
    </td><td>
      <b>Name</b>
    </td><td>
      <b>Description</b>
    </td><td>
      <b>Due Date</b>
    </td><td></td>
  </tr>

<?php foreach ($results as $row) {
     if ($row['completed'] == 0) { ?>
  <tr bgcolor="#FFFFFF">
    <td>
      <a href="list.php?complete_id=<?php print $row['id']; ?>"><b><img src="images/check.png" title="Mark as completed"></b></a>
<?php } else { ?>
  <tr bgcolor="#FF9999">
    <td>
      <a href="list.php?uncomplete_id=<?php print $row['id']; ?>"><b><img src="images/minus.png" title="Unmark as completed"></b></a>
<?php } ?>
    </td><td>
      <?php print $row['timestamp']; ?>
    </td><td>
      <?php print $row['entry_name']; ?>
    </td><td>
      <?php print $row['description']; ?>
    </td><td>
      <?php print $row['due_date']; ?>
    </td><td>
      <a href="list.php?remove_id=<?php print $row['id']; ?>"><b><img src="images/x.png" title="Delete"></b></a>
  </tr>

<?php
} ?>

</table>

<table><tr><td>
<span style="background-color:#FF9999;color:#000000;">Completed</span>
</td></tr></table>

<!-- Option to clear entire table.
<a href="list.php?clear=true">clear</a>
-->

<?php
} ?>

<br />
<a href="logout.php">Log out</a>
</body></html>
